//
//  TZHallViewController.m
//  wangYiLottery
//
//  Created by mac on 16/2/18.
//  Copyright © 2016年 tz. All rights reserved.
//




#import "TZHallViewController.h"
#import "TZHallCell.h"
#import "TZHallCellModel.h"
#import "TZButtonModel.h"

@interface TZHallViewController ()
@property (nonatomic, strong) NSArray *tempArray;
@property (nonatomic, strong) NSArray *scrollViewArray;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIView *coverView;
@property (nonatomic, strong) NSArray *buttonArray;
@end

@implementation TZHallViewController


static NSString *hallScrllViewID = nil;

//8个按钮的监听事件
- (void)fingerOneDidClick:(NSNotification *)not {
//    NSLog(@"%@",not.object);
//    NSLog(@"%@",not.object[0]);
//    NSString *str = [NSString stringWithFormat:@"%zd",not.object[0]];
//    NSInteger a = [str intValue];
//    NSLog(@"%zd",str);
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.navigationItem setTitle:@"购彩大厅"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addLeftButtonInNavItem];
    
    //添加通知 - 8个按钮的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fingerOneDidClick:) name:@"fingerOneDidClick" object:nil];
}

#pragma mark - UITableViewDatesoure
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tempArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = nil;
    TZHallCellModel *model = self.tempArray[indexPath.row];
    if ([model.title isEqualToString:@"0"]) {
        cellID = @"header";
        self.tableView.rowHeight = 140;
//        TZHallCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
//        cell.model = model;
        
        
    } else if ([model.title isEqualToString:@"1"]) {
        cellID = @"button";
        self.tableView.rowHeight = 180;
    } else {
        cellID = @"cell";
        self.tableView.rowHeight = 200;
    }
    TZHallCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    cell.model = model;
    
    TZButtonModel *buttonModel = self.buttonArray[indexPath.row];
    cell.buttonModel = buttonModel;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

//活动按钮
- (void)addLeftButtonInNavItem {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"CS50_activity_image"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(activity)];
}

//点击活动按钮调用
- (void)activity {
    //cover(遮罩)
    UIView *coverView = [[UIView alloc] init];
    //遮罩的大小
    coverView.frame = KSCREEN_SIZE;
    coverView.backgroundColor = [UIColor blackColor];
    coverView.alpha = 0.5;
    [self.tabBarController.view addSubview:coverView];
    self.coverView = coverView;

    //imageView
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"showActivity"]];
    imageView.center = CGPointMake(KSCREEN_WIDTH * 0.5, KSCREEN_HEIGHT * 0.5);
    [self.tabBarController.view addSubview:imageView];//如果加入到遮罩中，那么自己也会透明
    //    imageView.frame =
    self.imageView = imageView;

    //开启用户交互
    imageView.userInteractionEnabled = YES;

    //删除按钮
    UIButton *closeBtn = [[UIButton alloc] init];
    UIImage *closeImage = [UIImage imageNamed:@"alphaClose"];
    CGFloat W = closeImage.size.width;
    CGFloat H = closeImage.size.height;
    CGFloat X = imageView.bounds.size.width - W;
    CGFloat Y = 0;
    closeBtn.frame = CGRectMake(X, Y, W, H);
    //给button设施图片和状态
    [closeBtn setImage:closeImage forState:UIControlStateNormal];
    //监听
    [closeBtn addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:closeBtn];
}
//关闭按钮的监听事件
- (void)close {
    [UIView animateWithDuration:0.25 animations:^{
        self.imageView.alpha = 0;
        self.coverView.alpha = 0;
    } completion:^(BOOL finished) {
        [self.imageView removeFromSuperview];
        [self.coverView removeFromSuperview];
    }];
}

//懒加载
- (NSArray *)tempArray {
    if (!_tempArray) {
        NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"TZHallCell" ofType:@"plist"]];
        NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:array.count];
        for (NSDictionary *dict in array) {
            TZHallCellModel *model = [TZHallCellModel cellWithDict:dict];
            [mArray addObject:model];
        }
        _tempArray = mArray;
    }
    return _tempArray;
}

- (NSArray *)buttonArray {
    if (!_buttonArray) {
        NSArray *tempArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"TZHallButton.plist" ofType:nil]];
        NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:tempArray.count];
        for (NSDictionary *dict in tempArray) {
            TZButtonModel *model = [TZButtonModel buttomModelWithDict:dict];
            [mArray addObject:model];
        }
        _buttonArray = mArray;
    }
    return _buttonArray;
}

@end





